package com.cn.algorithm02.class04;

import java.util.Arrays;

/***
 * @author: hels
 * @description: 使用数组实现队列
 **/
public class C03_MyQueueWithArray {
     int[] arr;
     int limiesize; // 大小
     int size;

    int pointer; // 指针


    public static void main(String[] args) {
        C03_MyQueueWithArray myqueue = new C03_MyQueueWithArray(5);
        System.out.println(myqueue);
        myqueue.add(10);
        myqueue.add(11);
        myqueue.add(11);
        myqueue.add(11);
        myqueue.add(11);
        System.out.println(myqueue);
        myqueue.delete();
        myqueue.delete();
        myqueue.delete();
        System.out.println(myqueue);
        myqueue.add(20);
        System.out.println(myqueue);

    }

    public C03_MyQueueWithArray(int limiesize) {
        this.limiesize = limiesize;
        arr = new int[limiesize];
        size = 0;
        pointer = 0;
    }

    // add 放到尾巴上
    public void add(int node) {
        if (size == limiesize) {
            System.out.println("队列满了，不能增加");
            return;
        }

        arr[pointer] = node;
        size++;
        pointer = pointer+1 < limiesize ? ++pointer : 0;
    }

    // delete 删除第一个数据
    public void delete() {
        if (size == 0) {
            return;
        }

        if (pointer > size) {
            arr[pointer - size] = 0;
        } else {
            arr[pointer - size + limiesize] = 0;
        }
        size--;
    }

    public int size() {
        return size;
    }

    @Override
    public String toString() {
        for (int i = 0; i < limiesize; i++) {
            System.out.print(arr[i] + " ");
        }

        return "   C03_MyQueueWithArray{" +
                "size=" + size +
                ", pointer=" + pointer +
                '}';
    }


}
